//목록리스트 4단계 액티비티
package kr.jin.zeroprojectone;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class DBListAcitiviryStep4 extends Activity {
	ArrayList <MyItemsDBList4> arList4 = new ArrayList<MyItemsDBList4>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.dblist_activity_step4);
		
		setAdapterAndList();
		
	}
	
	private void setAdapterAndList() {
		DBHelper dbHelper= new DBHelper(this);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor c1 = db.rawQuery("SELECT * FROM step4", null);	
		c1.moveToFirst();	
		c1.getCount();
		
		MyItemsDBList4 dbItem4; 
		
		for(int i = 0; i<c1.getCount(); i++){		
			dbItem4 = new MyItemsDBList4(c1.getInt(0), c1.getString(1), c1.getString(2), c1.getLong(3), c1.getString(4), c1.getString(5));	
			arList4.add(dbItem4);
			c1.moveToNext();			
		}	
		DBListAdapterStep4 dblist4 = new DBListAdapterStep4(this, R.layout.dbcustomlistview4, arList4);
		ListView DBListStep4 = (ListView)findViewById(R.id.dblist_activity_step4_listview);
		DBListStep4.setAdapter(dblist4);
		DBListStep4.setOnItemClickListener(mItemListener);
		
		dbHelper.close();
	}
	
	AdapterView.OnItemClickListener mItemListener = new AdapterView.OnItemClickListener() {
		
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			showDeleteDalog(4,position);
				
		}
	};
	
	void showDeleteDalog(int num, int position){
			FragmentManager fm4 = getFragmentManager();
			FragmentTransaction tr4 = fm4.beginTransaction();
			Fragment fg4 = fm4.findFragmentByTag("delete4");
			if(fg4!=null){
				tr4.remove(fg4);
			}
			FragmentDialogDeleteList fd4 = FragmentDialogDeleteList.newInstance(num, position);
			fd4.show(tr4, "delete4");
	}
	
	
	void DeleteListPositiveDialog(int num, int position){
		DBHelper dbHelper= new DBHelper(DBListAcitiviryStep4.this);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		
		int i = arList4.get(position).id4;
		//int i = (Integer) parent.getAdapter().getItem(position);
		
		db.execSQL("DELETE FROM step4 WHERE _id = "+ i + ";");
		
		arList4.clear();
		dbHelper.close();
		setAdapterAndList();
	};
	
	
	
	
	
	
	
	
}
